import businessCommonMgr from '@/biz/businessReport/businessCommon'
import houseOpt from '@/biz/option/house.js'
import Opt from '@/components/dt/info/Opt.js'

let _this = null
let companys = []
class SearchOpt extends Opt {
  constructor(vm) {
    _this = vm
    super()
    this.build()
  }

  getCompany() {
    let opt = {
      title: '组织筛选',
      key: 'orgIds',
      placeholder: '全部',
      tree: {
        options: companys,
        saveAll: true
      },
      change: (v) => {
        const arr = v.editValue.filter((item) => item.level == 2)
        const arr2 = v.editValue.filter((item) => item.level == 3)

        if (arr.length > 1) {
          _this.levelValflag = false
        } else if (arr.length == 1) {
          _this.exportType = 2
          const parentId = arr[0].orgId
          const hasDifferentParent = arr2.some((item) => item.parentId != parentId)
          _this.levelValflag = !hasDifferentParent
        } else {
          _this.exportType = 3
          const parentIds = arr2.map((item) => item.parentId)
          const uniqueParentIds = new Set(parentIds)
          _this.levelValflag = uniqueParentIds.size == 1
        }
      }
    }
    let params = {
      isTwoLevel: true
    }
    businessCommonMgr.getOrgThreeTree(params).then((res) => {
      companys = res
      opt.tree.options = companys
    })
    return opt
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      this.getCompany(),
      houseOpt.create({
        title: '案场名称',
        placeholder: '请输入案场名称',
        to: (info) => {
          return {
            buildingName: info.housesName,
            buildingId: info.housesId
          }
        }
      })
    ]
  }
}

export default SearchOpt
